package com.lanou3g.demo.aspect;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TimeAspect {

    @Pointcut("execution(* com.lanou3g.demo.service.*.*(..))")
    public void allServiceMethods(){}

    @Around("allServiceMethods()")
    public Object time(ProceedingJoinPoint jp){
        try {
            long last = System.currentTimeMillis();
            Object result = jp.proceed();
            long time = System.currentTimeMillis() - last;
            Logger logger = Logger.getLogger((jp.getTarget().getClass()));
            logger.info((
                    jp.getTarget().getClass().getName() + "."
                    + jp.getSignature().getName()
                    + "执行了" + time + "毫秒"
                    ));
            return result;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;

    }
}
